//
//  HRZNumberOfChoiesView.m
//  EMarket
//
//  Created by 石头 on 2018/10/16.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZNumberOfChoiesView.h"

@interface HRZNumberOfChoiesView ()<UITextFieldDelegate, UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) UIButton *delBtn;
@property (nonatomic, strong) UITextField *numTF;

@property (nonatomic, weak) NSTimer *mTimer;

@end

@implementation HRZNumberOfChoiesView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.addBtn];
        [self addSubview:self.delBtn];
        [self addSubview:self.numTF];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.delBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
        make.width.mas_equalTo(self).multipliedBy(0.3);
    }];
    
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
        make.width.mas_equalTo(self).multipliedBy(0.3);
    }];
    
    [self.numTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self.delBtn.mas_right);
        make.bottom.mas_equalTo(self);
        make.right.mas_equalTo(self.addBtn.mas_left);
    }];
    
    self.layer.borderColor = [UIColor grayColor].CGColor;
    self.layer.borderWidth = .5f;
    self.layer.cornerRadius = 5;
}

#pragma mark - ----  Delegate  ----
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    _countNum = [textField.text numberValue];
    [self textFieldChange];
    return YES;
}

#pragma mark - ----  enentResponce  ----
//  增加一次
- (void)onNumAddOnce {
    _countNum = [NSNumber numberWithInt:[_countNum intValue] + 1];
    [self textFieldChange];
}

//  长按增加
- (void)onNumAddMore:(UILongPressGestureRecognizer *)guesture {
    [self numViewButtonClickIsAdd:YES withGuesture:guesture];
}

- (void)onNumDelOnce {
    if (![_countNum intValue]) {
        return;
    }
    _countNum = [NSNumber numberWithInt:[_countNum intValue] - 1];
    [self textFieldChange];
}

- (void)onNumDelMore:(UILongPressGestureRecognizer *)guesture {
    
    [self numViewButtonClickIsAdd:NO withGuesture:guesture];
}

//  修改数值
- (void)textFieldChange {
    _numTF.text = [_countNum stringValue];
    
    [self sendDelegateData];
}

- (void)numViewButtonClickIsAdd:(BOOL)isAdd withGuesture:(UILongPressGestureRecognizer *)guesture {
    
    BOOL flag = YES;
    if (guesture.state == UIGestureRecognizerStateEnded || guesture.state == UIGestureRecognizerStateFailed) {
        flag = NO;
    }
    
    BOOL isLongPressEnd = NO;
    if (guesture.state == UIGestureRecognizerStateEnded) {
        isLongPressEnd = YES;
    }
    
    if (flag) {
        if (!self.mTimer) {
            WEAKSELF
            if (@available(iOS 10.0, *)) {
                self.mTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 repeats:YES block:^(NSTimer * _Nonnull timer) {
                    if (isAdd) {
                        [weakSelf onNumAddOnce];
                    } else {
                        [weakSelf onNumDelOnce];
                    }
                }];
            } else {
                // Fallback on earlier versions
            }
        }
        
        [self.mTimer fire];
    }
    
    if (!flag) {
        if (self.mTimer.isValid) {
            [self.mTimer invalidate];
            self.mTimer = nil;
        }
    }
    
}

- (void)sendDelegateData {
    if (self.delegate && [self.delegate respondsToSelector:@selector(onCountChanged:)]) {
        [self.delegate onCountChanged:_countNum];
    }
}

#pragma mark - ----  Getter/Setter  ----

- (UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_addBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
        [_addBtn setImage:IMAGE_NAMED(@"icon_num_plus") forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(onNumAddOnce) forControlEvents:UIControlEventTouchUpInside];
        
        // 添加长按手势
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onNumAddMore:)];
        longPress.delegate = self;
        [_addBtn addGestureRecognizer:longPress];
    }
    return _addBtn;
}

- (UIButton *)delBtn{
    if (!_delBtn) {
        _delBtn = [[UIButton alloc] init];
        _delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _delBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_delBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
        [_delBtn setImage:IMAGE_NAMED(@"icon_num_minus") forState:UIControlStateNormal];
        [_delBtn addTarget:self action:@selector(onNumDelOnce) forControlEvents:UIControlEventTouchUpInside];
        
        // 添加长按手势
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onNumDelMore:)];
        longPress.delegate = self;
        [_delBtn addGestureRecognizer:longPress];
    }
    return _delBtn;
}

- (UITextField *)numTF{
    if (!_numTF) {
        _numTF = [[UITextField alloc] init];
        _numTF.delegate = self;
        
        _numTF.textAlignment = NSTextAlignmentCenter;
        _numTF.font = PFR10Font;
        _numTF.layer.borderWidth = .5f;
        _numTF.layer.borderColor = [UIColor grayColor].CGColor;
    }
    return _numTF;
}

- (void)setCountNum:(NSNumber *)countNum {
    if (countNum) {
        _countNum = countNum;
    } else {
        _countNum = @(4);
    }
    
   _numTF.text = [_countNum stringValue];
}

@end
